#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    m_App = new QTimer(this);

    ui->pushButton_monitor->setFocus();
    ui->pushButton_monitor->setDefault(true);

    connect(&mSysTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), // 最小化托盘
            this, SLOT(ActivateSystemTray(QSystemTrayIcon::ActivationReason)));
    connect(m_App, SIGNAL(timeout()), this, SLOT(HandleTimeOut()));

    CreateMenu(); // 创建菜单

}

Widget::~Widget()
{
    delete ui;
    delete menu;
    delete MainFace;
    delete Exit;
}

// 打开文件
void Widget::on_pushButton_open_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Choose File"), NULL, tr("txtFile (*.exe)"));
    if(!fileName.isEmpty()) {
        ui->lineEdit->setText(fileName);
        ui->textEdit_info->append("选择了路径...");
    }
}

// 监控
void Widget::on_pushButton_monitor_clicked()
{
    QString path = ui->lineEdit->text();
    if(path.isEmpty()) {
        ui->textEdit_info->append("应用程序路径为空！！");
        return;
    }
    QStringList pn = path.split("/");
    AppName = "imagename eq ";
    foreach (QString tmp, pn) {
        if(!tmp.contains(".exe")) continue;
        AppName += tmp;
        break;
    }
    if(!AppName.isEmpty()) {

        arg << "-fi" << AppName;
        pn.clear();
    }
    else
        return;
    if(ui->pushButton_monitor->text() == "开始监控") {
        ui->pushButton_monitor->setText("停止监控");
        ui->textEdit_info->append("开始监控");
        ui->lineEdit->setEnabled(false);
        ui->pushButton_open->setEnabled(false);
        if(!m_App->isActive())
            m_App->start(5000);
    }
    else {
        ui->pushButton_monitor->setText("开始监控");
        ui->textEdit_info->append("停止监控");
        ui->lineEdit->setEnabled(true);
        ui->pushButton_open->setEnabled(true);
        if(m_App->isActive())
            m_App->stop();
    }
}

// 程序信息编辑结束
void Widget::on_lineEdit_editingFinished()
{
    QString path = ui->lineEdit->text();
    if(path == "") {
        ui->textEdit_info->append("路径不能为空，请选择路径！");
        return;
    }
    if(!path.contains(".exe")) {
        ui->textEdit_info->append("路径错误,请检查！");
        return;
    }
}


void Widget::closeEvent(QCloseEvent *e)
{
    if(QMessageBox::information(NULL, QString("提示"), QString("是否最小化托盘?\n\n选择是最小化托盘，选择否退出"),
                    QMessageBox::Ok | QMessageBox::No) == QMessageBox::Ok) {
        e->ignore();
        MiniTray();
    }
    else {
        if(m_App->isActive())
            m_App->stop();
        e->accept();
    }
}

void Widget::MiniTray()
{
    this->hide();

    mSysTrayIcon.setIcon(QIcon("./Monitor.png")); //将icon设到QSystemTrayIcon对象中
    mSysTrayIcon.setToolTip("检测软件启动"); //当鼠标移动到托盘上的图标时，会显示此处设置的内容
    mSysTrayIcon.show(); //在系统托盘显示此对象
    mSysTrayIcon.showMessage("提示", "单击或双击显示界面");
}

void Widget::ActivateSystemTray(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Trigger:
    {
        mSysTrayIcon.hide();
        this->show();
    }
        break;
    case QSystemTrayIcon::DoubleClick:
    {
        mSysTrayIcon.hide();
        this->show();
    }
        break;
    default:
        break;
    }
}

void Widget::CreateMenu()
{
    MainFace = new QAction("主界面", this);
    connect(MainFace, SIGNAL(triggered()), this, SLOT(MainInterface()));

    Exit = new QAction("退出", this);
    connect(Exit, SIGNAL(triggered()), this, SLOT(ExitProgram()));

    menu = new QMenu(this);
    menu->addAction(MainFace);
    menu->addAction(Exit);
    mSysTrayIcon.setContextMenu(menu);
}

void Widget::MainInterface()
{
    this->show();
    mSysTrayIcon.hide();
}

void Widget::ExitProgram()
{
    this->close();
}

void Widget::HandleTimeOut()
{
    static long cnt = 0;
    QString str, info;
    QString cmd = "tasklist";
    QProcess StartSW(this);
    str = QString("第%1次检测").arg(cnt);
    StartSW.start(cmd, arg);
    StartSW.waitForFinished();
    info = QString::fromLocal8Bit(StartSW.readAllStandardOutput());
    if(info.contains("没有运行的任务匹配指定标准")){
        StartSW.startDetached(ui->lineEdit->text(), QStringList(ui->lineEdit->text()));
        ui->textEdit_info->append(str + ", 没有见到程序运行，正在开启程序...");
    }
    else {
        ui->textEdit_info->append(str + ", 程序已经在运行...");
    }
    if(ui->textEdit_info->document()->lineCount() > 9999)
        ui->textEdit_info->clear();
    cnt++;
}




